package com.dianban.utils;

import com.dianban.config.redis.RedisConstant;
import com.dianban.config.redis.RedisUtil;
import com.dianban.entity.ShopUser;
import com.dianban.entity.User;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wujh
 * @version 2020/6/29
 */
public class UserUtils {

    public static final String USER_TOKEN="userToken";
    public static final String SHOP_USER_TOKEN="shopUserToken";

    public static User getUser(HttpServletRequest request){
        String userToken = request.getHeader(USER_TOKEN);
        if(StringUtils.isBlank(userToken)){
            return null;
        }
        String key = RedisConstant.USER_TOKEN_KEY+userToken;
        User user = (User) RedisUtil.get(key);
        return user;
    }

    public static void logoutUser(HttpServletRequest request){
        String userToken = request.getHeader(USER_TOKEN);
        if(StringUtils.isNotBlank(userToken)){
            String key = RedisConstant.USER_TOKEN_KEY+userToken;
            RedisUtil.del(key);
        }
    }

    public static ShopUser getShopUser(HttpServletRequest request){
        String shopUserToken = request.getHeader(SHOP_USER_TOKEN);
        if(StringUtils.isBlank(shopUserToken)){
            return null;
        }
        String key = RedisConstant.SHOP_USER_TOKEN_KEY+shopUserToken;
        ShopUser shopUser = (ShopUser) RedisUtil.get(key);
        return shopUser;
    }

    public static void logoutShopUser(HttpServletRequest request){
        String shopUserToken = request.getHeader(SHOP_USER_TOKEN);
        if(StringUtils.isNotBlank(shopUserToken)){
            String key = RedisConstant.USER_TOKEN_KEY+shopUserToken;
            RedisUtil.del(key);
        }
    }

    public static String getScreenMac(HttpServletRequest request){
        return request.getHeader("screenMac");
    }

}
